package com.tjy.framework.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

import java.util.Random;

// SmsService.java
@Service
public class SmsService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${sms.expire-time:5}")
    private int expireTime; // 验证码过期时间（分钟）

    public boolean sendVerificationCode(String phone) {
        // 1. 生成 6 位随机验证码
        String code = String.format("%06d", new Random().nextInt(999999));

        // 2. 存入 Redis：key = sms:login:13800138000, value = 123456, 过期 5 分钟
        String key = "sms:login:" + phone;
        redisTemplate.opsForValue().set(key, code, expireTime, TimeUnit.MINUTES);

        // 3. 调用短信平台发送（伪代码）
        boolean success = sendSmsByAliyun(phone, "您的验证码是：" + code);

        return success;
    }

    private boolean sendSmsByAliyun(String phone, String content) {
        // 这里对接阿里云 SDK 或调用 HTTP API
        // 参考阿里云短信服务文档
        return true; // 发送成功
    }

    /**
     * 发送短信
     * @param phoneNum
     * @param s
     * @return
     */
    public boolean sendSms(String phoneNum, String s) {
        return true;
    }
}